package com.ff.xyh.service.post.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ff.xyh.common.entity.post.PostDelete;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.qo.post.PostDeleteQuery;
import com.ff.xyh.service.post.mapper.PostDeleteMapper;
import com.ff.xyh.service.post.service.PostDeleteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 帖子删除表 服务实现类
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@Service
public class PostDeleteServiceImpl extends ServiceImpl<PostDeleteMapper, PostDelete> implements PostDeleteService {

    @Override
    public ResultObject getPostDeleteCondition(PostDeleteQuery postDeleteQuery) {


        Page<PostDelete> page = new Page<>(postDeleteQuery.getCurrentPage(), postDeleteQuery.getPageSize());
        List<PostDelete> resultList = new ArrayList<>();
        QueryWrapper<PostDelete> wrapper = new QueryWrapper<>();


        if (postDeleteQuery.getGmtAfter() != null) {
            wrapper.ge("gmt_created", postDeleteQuery.getGmtAfter());
        }
        if (postDeleteQuery.getGmtBefore() != null) {
            wrapper.le("gmt_created", postDeleteQuery.getGmtBefore());
        }
        // 按创建时间排序意味着要最新
        if (postDeleteQuery.getOrderByCreatedTime() != null && postDeleteQuery.getOrderByCreatedTime()) {
            // 降序
            wrapper.orderByDesc("gmt_created");
        } else {
            wrapper.orderByAsc("gmt_created");
        }


        if (!StringUtils.isEmpty(postDeleteQuery.getPostId())) {
            wrapper.eq("post_id", postDeleteQuery.getPostId());
        }
        if (postDeleteQuery.getPostType() != null) {
            wrapper.eq("post_type", postDeleteQuery.getPostType());
        }
        if (!StringUtils.isEmpty(postDeleteQuery.getOperatorId())) {
            wrapper.eq("operator_id", postDeleteQuery.getOperatorId());
        }

        IPage<PostDelete> resultPage = this.page(page, wrapper);

        return ResultObject.ok().putList(resultPage.getRecords(), resultPage.getTotal());

    }
}
